package com.atguigu.yygh.hosp.lisenter;

import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.mq.constant.MessageQueueContant;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.order.OrderMqVo;
import org.aspectj.bridge.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 监听队列
 */
@Component //将当前类放到ioc容器
public class OrderMqListener {

    @Autowired
    private ScheduleService scheduleService; //去mongo跟新医生剩余可预约数
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = MessageQueueContant.QUEUE_ORDER)
    public void consume(OrderMqVo orderMqVo,Message message){
        String scheduleId = orderMqVo.getScheduleId(); //排班id
        Integer availableNumber = orderMqVo.getAvailableNumber();//剩余可预约数

        scheduleService.updateAvailableNumber(scheduleId,availableNumber);
        MsmVo msmVo = orderMqVo.getMsmVo();
        if(msmVo != null){
            rabbitTemplate.convertAndSend(MessageQueueContant.EXCHANGE_SMS, MessageQueueContant.ROUTING_KEY_SMS,msmVo);
        }
    }
}
